package service.impl;

import dao.JobholderDao;
import domain.Jobholder;
import service.JobholderService;
import service.RegisterService;
import util.CheckUtil;
import util.MyBatisUtil;

import java.util.List;

public final class JobholderServiceImpl implements JobholderService, RegisterService {
    private JobholderServiceImpl() {}
    private static final JobholderServiceImpl INSTANCE = new JobholderServiceImpl();
    private static final JobholderDao jobholderDao = MyBatisUtil.getMapper(JobholderDao.class);
    public static JobholderServiceImpl getINSTANCE() { return INSTANCE; }

    @Override
    public int register(Jobholder jobholder) {
        return jobholderDao.insert(jobholder);
    }

    @Override
    public int resetJobholder(Jobholder jobholder) {
        return jobholderDao.update(jobholder);
    }

    @Override
    public Jobholder login(String tel, String password) {
        Jobholder jobholder=jobholderDao.selectByTel(tel);
        if(jobholder !=null && password.equals(jobholder.getPassword()))
            return jobholder;
        return null;
    }

    @Override
    public Jobholder selectById(Long id) {
        return jobholderDao.selectById(id);
    }

    @Override
    public Jobholder selectByTel(String tel) {
        return jobholderDao.selectByTel(tel);
    }

    @Override
    public int verifyRegisterTel(String tel) {
        if(!CheckUtil.checkTel(tel)){
            return 1;
        }
        if(selectByTel(tel)!=null){
            return 2;
        }
        return 0;
    }

    @Override
    public int verifyRegisterName(String name) {
        if(CheckUtil.checkName(name)){
            return 1;
        }
        if(jobholderDao.selectByName(name)!=null){
            return 2;
        }
        return 0;
    }

    @Override
    public List<Jobholder> selectAll() {
        return jobholderDao.selectAll();
    }
}
